package com.gl.utils;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Component
public class AliyunOSSOperator {

    private String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    private String bucketName = "keshe-travel-guide";
    private String region = "cn-beijing";
    private String accessKeyId = "LTAI5tHQ4czLPK52LGDMFqcz";//""LTAI5t78YwiLzcorT1eipsoY";
    private String accessKeySecret = "8XvrJrBpgR6tsD2772MPaVklBbN8nx";//"dlFJuXsdNhiBu3a9wibqeH8TarG3fh";

    public String upload(byte[] content, String originalFilename) throws Exception {
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 显式设置accesskeyId和accesskeySecret
        DefaultCredentialProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);

        // 填写Object完整路径，例如202406/1.png。Object完整路径中不能包含Bucket名称。
        //获取当前系统日期的字符串,格式为 yyyy/MM
        String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
        //生成一个新的不重复的文件名
        String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectName = dir + "/" + newFileName;

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);

        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
        } finally {
            ossClient.shutdown();
        }

        return endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + objectName;
    }

    public void deleteImageByUrl(String imageUrl) throws Exception {
        // 解析URL，获取bucket名称和objectName
        String[] parts = imageUrl.split("/");
        String domain = parts[2]; // 例如: keshe-travel-guide.oss-cn-beijing.aliyuncs.com
        String[] bucketDomainParts = domain.split("\\.");
        String actualBucketName = bucketDomainParts[0]; // 获取bucket名称

        // 确保bucket名称与我们配置的bucket名称一致
        if (!actualBucketName.equals(this.bucketName)) {
            throw new Exception("Bucket name in URL does not match the configured bucket name.");
        }

        // 获取objectName
        String objectName = parts[3]; // 跳过endpoint部分，获取实际的objectName
        for (int i = 4; i < parts.length; i++) {
            objectName += "/" + parts[i];
        }

        // 创建OSSClient实例。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        DefaultCredentialProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();

        try {
            ossClient.deleteObject(bucketName, objectName);
        } finally {
            ossClient.shutdown();
        }
    }

}
